package ufpb.compiladores.sintatico;

import java.util.HashMap;

/**
 *
 * @author SemiNick
 */
public abstract class Enviroment {

    private HashMap<String, Production> productions = new HashMap<String, Production>();
    private Production initialProduction = null;
    
    public Enviroment() {
    }
    
    public void addProduction(Production p) {
        
        this.addProduction(p.getName(), p);
    }
    
    public void addProduction(String name, Production p) {
        
        if (p == null || name == null) {
            
            throw new NullPointerException("Production cannot be null");
        } else if (name.equals("")) {
            
            throw new IllegalArgumentException("Name cannot be empty");
        }

        this.productions.put(name, p);
    }
   
    public Production getProduction(String name) {
        
        return this.productions.get(name);
    }
    
    public void removeProduction(String name) {
        
        this.productions.remove(name);
    }
    
    public Production[] getAllProductions() {
        
        return this.productions.values().toArray(new Production[0]);
    }
    
    public void setInitialProduction(Production p) {
        
        if (this.getProduction(p.getName()) == null) {
            
            throw new IllegalArgumentException("Initial production must be in Enviroment");
        }
        
        this.initialProduction = p;
    }
    
    public void setInitialProduction(String name) {
        
        this.setInitialProduction(this.getProduction(name));
    }

    public final Production getInitialProduction() {
        
        return this.initialProduction;
    }
    
    public abstract void removeRecursion();

    public abstract void removeNonDeterminism();
}
